package cj.web.admin.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

public class GeneratePdf {

	/**
	 * 生成PDF文件（此类要外部引用“itextpdf.jar”，下载地址为：http://www.java2s.com/Code/Jar/i/itextpdf
	 * .htm）
	 * @param files		文件组
	 * @param dir_save	PDF另存路径
	 * @throws DocumentException
	 * @throws IOException
	 */
	public static void creatPDF(File[] files, String dir_save)
			throws DocumentException, IOException {
		// 标准A4的宽
		float A4_weight = 595;
		// 标准A4的高
		float A4_height = 842;

		Document document = new Document(PageSize.A4, 0, 0, 1, 0);
		// 创建文档容器
		PdfWriter.getInstance(document, new FileOutputStream(dir_save));
		// 创建编写器（PDF类型）
		document.open();
		// 打开容器

		float percent = 100;
		float w, h;
		for (int i = 0; i < files.length; i++) {

			Image img = Image.getInstance(files[i].getCanonicalPath());

			/* 处理图片缩放比例 */
			w = img.getWidth();
			h = img.getHeight();
			if ((w > A4_weight) && (h < A4_height))
				percent = (A4_weight * 100) / w;
			else if ((w < A4_weight) && (h > A4_height))
				percent = (A4_height * 100) / h;
			else if ((w > A4_weight) && (h > A4_height)) {

				percent = (A4_weight * 100) / w;
				h = (h * percent) / 100;
				if (h > A4_height)
					percent = (A4_height * 100) / h;
			}

			img.scalePercent(percent);
			document.add(img);
		}

		document.close();
		// 关闭容器
	}

	public static void main(String[] args) throws DocumentException,
			IOException {
		// TODO 自动生成的方法存根
		String path = "E:\\11\\";
		File[] files = new File[1];
		String dir_save = "E:\\11\\AIRvance.pdf";

		File file1 = new File(path, "AIRvance.jpg");
//		File file2 = new File(path, "2.jpg");
//		File file3 = new File(path, "3.jpg");
//		File file4 = new File(path, "4.jpg");
//		File file5 = new File(path, "5.jpg");
//		File file6 = new File(path, "6.jpg");
//		File file7 = new File(path, "7.jpg");
//		File file8 = new File(path, "8.jpg");
//		File file9 = new File(path, "9.jpg");
//		File file10 = new File(path, "10.jpg");
//		File file11 = new File(path, "11.jpg");
//		File file12 = new File(path, "12.jpg");
//		File file13= new File(path, "13.jpg");
//		File file14 = new File(path, "14.jpg");
//		File file15 = new File(path, "15.jpg");
//		File file16 = new File(path, "16.jpg");
//		File file17 = new File(path, "17.jpg");
//		File file18 = new File(path, "18.jpg");
		
		files[0] = file1;
//		files[1] = file2;
//		files[2] = file3;
//		files[3] = file4;
//		files[4] = file5;
//		files[5] = file6;
//		files[6] = file7;
//		files[7] = file8;
//		files[8] = file9;
//		files[9] = file10;
//		files[10] = file11;
//		files[11] = file12;
//		files[12] = file13;
//		files[13] = file14;
//		files[14] = file15;
//		files[15] = file16;
//		files[16] = file17;
//		files[17] = file18;
		
		creatPDF(files, dir_save);
	}

}